package com.ailk.kbi.kbirazor.component
{
	import com.ailk.kbi.kbirazor.common.CONTENT;
	import com.ailk.kbi.kbirazor.model.KbrModel;
	
	import flash.display.Graphics;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Alert;
	import mx.core.IVisualElement;
	
	import spark.components.BorderContainer;
	import spark.components.HGroup;
	import spark.components.Label;
	import spark.components.VGroup;
	import spark.layouts.VerticalLayout;
	
	
	
	/**
	 * 
	 * 节点
	 */
	public class Node extends BorderContainer{
		private var _contentXML:XML;
		
		/*当前容器*/
		private var _nodeRow:NodeRow;
		
		private var _vgMain:VGroup;
		
		private var selectedFlag:Boolean;
		[Bindable]
		private var kbrModel:KbrModel = KbrModel.getInstance();
		
		public function Node(contentXML:XML)
		{
			super();	
			_contentXML = contentXML;
			updateChildren();
		}
		
		public function get yMidden():Number{
			return height / 2;
		}
		
		private function updateChildren():void{
			var verticalLayout:VerticalLayout = new VerticalLayout();
			with(verticalLayout){
				paddingTop = 5;
				paddingLeft = 10;
				paddingRight = 10;
				paddingBottom = 5;
			}
			layout = verticalLayout;
			
			_vgMain = new VGroup();
			addElement(_vgMain);
			
			createNewLine();
			
			selectedFlag = _contentXML.hasOwnProperty('@selected') && _contentXML.@selected == true;
			//			Alert.show("是否为："+(_contentXML.@selected == true));
			//			this.setStyle("borderColor","#B78306"); 
			this.setStyle("borderWeight","2"); 
			this.setStyle("cornerRadius","15"); 
			this.setStyle("backgroundColor","#b4e5fd");
			this.setStyle("dropShadowVisible",true); 
			
			if(selectedFlag) {
				this.setStyle("borderColor","#00aaff");
//				this.setStyle("color","#00aaff");
				kbrModel.nodeSelectedNum = 1;
			} else {
				this.setStyle("borderColor","#006da3");
				this.setStyle("color",CONTENT.COLOR_NORMAL);
			}
			
			kbrModel.addEventListener("clickChange",clickChangeHandler);
			this.addEventListener(MouseEvent.CLICK, clickHandler);
			
			updateInner();
		}
		
		/*更新内部元素*/
		private function updateInner():void{
			if(_contentXML.hasOwnProperty('inner')){
				var innerXML:XML = XML(_contentXML.inner);
				for each(var innerChildXML:XML in innerXML.*){
					var xmlName:String = innerChildXML.name();
					switch(xmlName){
						case 'kpi':
							updateKpi(innerChildXML);
							break;
						case 'label':
							updateLabel(innerChildXML);
							break;
						case 'br':
							updateBreak(innerChildXML);
							break;
						default:
					}
				}
			}
		}
		
		/*更新KPI*/
		private function updateKpi(kpiXML:XML):void{
			var kpiBox:KpiBox = new KpiBox(kpiXML);
			_nodeRow.addElement(kpiBox);
		}
		
		private function updateLabel(labelXML:XML):void{
			var label:Label = new Label();
			label.text = labelXML.@text;
			_nodeRow.addElement(label);
		}
		
		private function updateBreak(brXML:XML):void{
			createNewLine();
		}
		
		private function createNewLine():void{
			_nodeRow = new NodeRow();
			_vgMain.addElement(_nodeRow);
			_nodeRow.addEventListener("widthChanged", widthChangedHandler);
			_nodeRow.addEventListener("heightChanged", heightChangedHandler);
		}
		
		private function widthChangedHandler(event:Event):void {
			var newWidth:Number = 0;
			for(var index:int = 0; index < this.numElements; index++){
				var element:IVisualElement = this.getElementAt(index);
				newWidth = Math.max(newWidth, element.width);
			}
			_vgMain.width = newWidth;
			this.width = _vgMain.width + 24;
		}
		

		private function heightChangedHandler(event:Event):void {
			var newHeigth:Number = (this.numElements - 1) * _vgMain.gap;
			for(var index:int = 0; index < this.numElements; index++){
				var element:IVisualElement = this.getElementAt(index);
				newHeigth = newHeigth + element.height;
			}
			_vgMain.height = newHeigth;
			this.height = _vgMain.height + 14;
		}
		
		protected function clickChangeHandler(event:MouseEvent):void{
			
			this.setStyle("color","black");
			this.setStyle("borderColor","#006da3"); 
			selectedFlag = false;
			kbrModel.nodeSelectedNum = 0;
			_contentXML.@selected = false;
		}
		
		protected function clickHandler(event:MouseEvent):void
		{
			if(!selectedFlag && kbrModel.nodeSelectedNum == 0) {
//				this.setStyle("color","#00aaff");
				this.setStyle("borderColor","#00aaff"); 
				selectedFlag = true;
				kbrModel.nodeSelectedFlag = true;
				kbrModel.nodeSelectedNum = 1;
				_contentXML.@selected = true;
			} 
			else if(!selectedFlag && kbrModel.nodeSelectedNum == 1) {
				kbrModel.clickFocus();
				clickHandler(event);
			} else if(selectedFlag && kbrModel.nodeSelectedNum == 1) {
				this.setStyle("color","black");
				this.setStyle("borderColor","#006da3"); 
				selectedFlag = false;
				kbrModel.nodeSelectedFlag = false;
				kbrModel.nodeSelectedNum = 0;
				_contentXML.@selected = false;
			}
		}
	}
}